#include "include.h"
#include "pid.h"
#include <signal.h>
#include <stdlib.h>

void SAMPLE_VIO_HandleSig(int signo)
{
    if (SIGINT == signo || SIGTERM == signo)
    {
        vdDeInit_MCU_GPIO();//init  the mcu gpio
        vdTimer1_deinit();
        printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
    }
    exit(-1);
}
int main( void )
{
    int ret;

    data_received[0] = 0x00;
    data_received[1] = 0x00;

    printf("Schips 20230910\n");

    //I2c_Select    = 0x01;
    I2CADR      = LT86101UXE;
    vdInit_MCU_GPIO();//init  the mcu gpio
    vdInitialGlobal();//Initialize global variables
    Init_I2C(6);
    vdTimer1_Delay10ms();//timer1 to 10ms
    InitialUART1_Timer3( 115200 );

    signal(SIGINT, SAMPLE_VIO_HandleSig);
    signal(SIGTERM, SAMPLE_VIO_HandleSig);

    runOnce("/tmp/.lt86101", RUNONCE_KILL_ELDERSHIP);

    while ( 1 )
    {
        vdChip_Init();//reset chip
        vdMcu_SystemReset();//when g_ulcurrenttime >0xffffff00,reset the muc
        while ( g_Flag_Init_Finished )
        {
            ret = vStart_work();
            ret += CLK_det();
            ret += vdClk_Stable_Config();
            //if(ret == 3)
            //{
            //    goto append;
            //}
            vdPrintError();//used to print the error conter,it can show the input signal quality
        }
    }

append :

    printf("\nLT86101 Init ALL DONE\n\n");
    vdTimer1_deinit();
    vdDeInit_MCU_GPIO();//init  the mcu gpio
    return 0;
}

